package org.cresource.pet.service.impl;

import org.cresource.basic.domain.Point;
import org.cresource.basic.service.impl.BaseServiceImpl;
import org.cresource.basic.util.CodeGenerateUtils;
import org.cresource.basic.util.DistanceUtil;
import org.cresource.basic.util.PageList;
import org.cresource.basic.util.SmsUtil;
import org.cresource.order.domain.PetAcquisitionOrder;
import org.cresource.order.mapper.PetAcquisitionOrderMapper;
import org.cresource.org.domain.Employee;
import org.cresource.org.domain.Shop;
import org.cresource.org.mapper.EmployeeMapper;
import org.cresource.org.mapper.ShopMapper;
import org.cresource.pet.domain.Pet;
import org.cresource.pet.domain.PetDetail;
import org.cresource.pet.domain.SearchMasterMsg;
import org.cresource.pet.mapper.PetMapper;
import org.cresource.pet.mapper.SearchMasterMsgMapper;
import org.cresource.pet.query.SearchMasterMsgQuery;
import org.cresource.pet.service.IPetService;
import org.cresource.pet.service.ISearchMasterMsgService;
import org.cresource.user.domain.LoginInfo;
import org.cresource.user.domain.User;
import org.cresource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

@Autowired
    SearchMasterMsgMapper searchMasterMsgMapper;
@Autowired
ShopMapper shopMapper;
@Autowired
UserMapper userMapper;
@Autowired
EmployeeMapper employeeMapper;
@Autowired
IPetService petService;
@Autowired
PetAcquisitionOrderMapper petAcquisitionOrderMapper;
@Autowired
PetMapper petMapper;

    /**
     * 发布寻主消息
     * 发布寻主消息
     * 发布寻主消息
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, LoginInfo loginInfo) {
//        1.找到最近的门店 绑定门店
        //        1.计算发布寻主消息人与的位置
        //        1.首先拷贝地图的工具类  改jk
        //        2.getPoint   adress
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
//        2.计算出最近的门店
        //        1.getNearestShop  本地址 店铺list  loadall xml没有
        Shop shop = DistanceUtil.getNearestShop(point, shopMapper.loadAllShop());
        //        2.set门店
        searchMasterMsg.setShop(shop);
        //        3.set门店id
        searchMasterMsg.setShop_id(shop.getId());
//        3.绑定发布的user
        //         1.通过id得到user
        Long id = loginInfo.getId();
        User user = userMapper.loadByloingInfoId(id);
        //        2.setuser
        searchMasterMsg.setUser(user);
        //        3.setuserid
        searchMasterMsg.setUser_id(user.getId());
//        4.存储寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
//                save
//        5.发送消息给管理员
//        SmsUtils.sendSms
        System.out.println("来新单了");

    }
    /**
         * 未处理
         * 未处理
         * 未处理
         */
        @Override
        public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
            //未处理 状态是下架状态
            query.setState(0);
            //通过logininfoid 查employee的shop_id字段 有是店铺 没有就是管理
            Employee employee= employeeMapper.loadByLoginInfoId(id);
            //店铺要把shopid=#{shopid}当条件给xml判断 管理员不需要
            if (employee.getShop_id()!=null){
                query.setShopId(employee.getShop_id());
            }
            //查询
            return super.queryPage(query);
    }


    /**
     * 处理
     */
    @Override
    @Transactional
    public void handle(Pet pet,Long loginInfoId) {
        // 修改寻主消息状态
        searchMasterMsgMapper.updateStateForProcessed(pet.getSearch_master_msg_id());
        // 生成宠物基本信息 -- 返回宠物的自增id
        // 生成宠物详情
        petService.add(pet);
        // 生成收购订单
        // 获取当前员工的信息
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        // 获取当前寻主的消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        // 调用工具类生成订单的信息
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
        // 把订单保存到数据库
        petAcquisitionOrderMapper.save(petAcquisitionOrder);

    }

    /**
     * 生成收购订单的信息
     * @param pet
     * @param adopt
     * @param empId
     * @return
     */
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id()); // 订单唯一标识
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }








    /**
     * 已处理消息
     * 已处理消息
     * 已处理消息
     */
    @Override
    public PageList<SearchMasterMsg> detail(SearchMasterMsgQuery query, Long id) {
        //未处理 状态是下架状态
        query.setState(1);
        //通过logininfoid 查employee的shop_id字段 有是店铺 没有就是管理
        Employee employee= employeeMapper.loadByLoginInfoId(id);
        //店铺要把shopid=#{shopid}当条件给xml判断 管理员不需要
        if (employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        System.out.println(query);
        //查询
        return super.queryPage(query);
    }


}
